package com.jic.manager.controller.manage.conf;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.CmsTemplate;
import com.jic.manager.entity.vo.request.CmsTemplateRequest;
import com.jic.manager.entity.vo.response.CmsTemplateResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.CmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

;

/**
 * @Author: Taozhouchuan
 * @date: 2020-03-22 01:47:51
 * NFZD TODO 本页面需要修改package的路径；
 */
@RestController
@Slf4j
@RequestMapping("/cmsTemplate")
@Api(tags = "控制类-PC")
public class CmsTemplateController {
    @Autowired
    private CmsTemplateService service;

    /**
     * CMS_TEMPLATE 查询列表（分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listPage")
    @ApiOperation(value = "分页查询cmsTemplate列表")
    @ResponseBody
    public RestResult<PageResult<CmsTemplateResponse>> listCmsTemplateRecordPagination(@RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        PageResult<CmsTemplateResponse> pageResult = new PageResult<>();
        CmsTemplate cmsTemplate = new CmsTemplate();
        try {
//			BeanUtils.copyProperties(request, cmsTemplate);
            List<CmsTemplateResponse> list = service.listCmsTemplateRecordPagination(request);
            long count = service.listPageCount(cmsTemplate);
            List<CmsTemplateResponse> responseList = list.stream().map(temp -> {
                CmsTemplateResponse bo = new CmsTemplateResponse();
                BeanUtils.copyProperties(temp, bo);
                return bo;
            }).filter(item -> item != null).collect(Collectors.toList());
            pageResult.setRows(responseList);
            pageResult.setTotal(count);
        } catch (Exception e) {
            log.info(">>>>>>>>>>查询cmsTemplate列表列表异常!>>>>>>>>>>");
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return RestResult.success(pageResult);
    }

    /**
     * CMS_TEMPLATE 查询cmsTemplate列表列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询cmsTemplate列表")
    @ResponseBody
    public RestResult<List<CmsTemplateResponse>> listCmsTemplateAllRecord(@RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        CmsTemplateRequest cmsTemplateRequest = new CmsTemplateRequest();
        CmsTemplate cmsTemplate = new CmsTemplate();
        BeanUtils.copyProperties(request, cmsTemplate);
        List<CmsTemplate> list = service.listCmsTemplateAllRecord(cmsTemplate);
        List<CmsTemplateResponse> responseList = list.stream().map(temp -> {
            CmsTemplateResponse bo = new CmsTemplateResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 单表查询cmsTemplate列表详情信息
     *
     * @param request 查询cmsTemplate列表详情请求参数
     * @return CmsTemplateResponse 查询cmsTemplate列表详情响应参数
     * author Taozhouchuan
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/viewCmsTemplate")
    @ApiOperation(value = "查询cmsTemplate详情")
    @ResponseBody
    public RestResult<CmsTemplateResponse> getCmsTemplateDetail(@Validated @RequestBody CmsTemplateRequest request) {
        log.info("查询cmsTemplate详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null) {

            CmsTemplate cmsTemplate = new CmsTemplate();
            BeanUtils.copyProperties(request, cmsTemplate);
            CmsTemplate result = service.getCmsTemplate(cmsTemplate);
            log.info("查询cmsTemplate详情请求参数出参，{}", JSONObject.toJSONString(result));
            Optional.ofNullable(result).orElseThrow(() -> new AppRuntimeException(ResponseCode.EXCEPTION, "记录不存在"));
            CmsTemplateResponse bo = new CmsTemplateResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询cmsTemplate详情请求结束");
        return RestResult.success(new CmsTemplateResponse());
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改请求参数
     * @return CmsTemplateResponse 新增或修改响应参数
     * author Taozhouchuan
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新cmsTemplate")
    @PostMapping("/saveOrUpdateCmsTemplate")
    @ResponseBody
    public RestResult<Long> saveOrUpdateCmsTemplate(@Validated @RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        CmsTemplate cmsTemplate = new CmsTemplate();
        BeanUtils.copyProperties(request, cmsTemplate);
        long flag = service.saveOrUpdateCmsTemplate(cmsTemplate);
        if (flag > 0) {
            return RestResult.success(flag);
        } else {
            return RestResult.error("9999", "新增或修改异常");
        }
    }

    /**
     * 修改记录
     *
     * @param request 修改请求参数
     * @return CmsTemplateResponse 修改响应参数
     * author Taozhouchuan
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改cmsTemplate记录")
    @PostMapping("/updateCmsTemplate")
    @ResponseBody
    public RestResult<Long> updateCmsTemplate(@RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        CmsTemplate cmsTemplate = new CmsTemplate();
        BeanUtils.copyProperties(request, cmsTemplate);
        long flag = service.updateCmsTemplate(cmsTemplate);
        if (flag > 0) {
            return RestResult.success(flag);
        } else {
            return RestResult.error("9999", "修改异常");
        }
    }
// 注释

    /**
     * 新增记录
     *
     * @param request 新增请求参数
     * @return CmsTemplateResponse 新增响应参数
     * author Taozhouchuan
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增cmsTemplate记录")
    @PostMapping("/addCmsTemplate")
    @ResponseBody
    public RestResult<Long> addCmsTemplate(@RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        CmsTemplate cmsTemplate = new CmsTemplate();
        BeanUtils.copyProperties(request, cmsTemplate);
        long flag = service.addCmsTemplate(cmsTemplate);
        if (flag > 0) {
            return RestResult.success(flag);
        } else {
            return RestResult.error("9999", "新增异常");
        }
    }

    /**
     * 删除记录
     *
     * @param request 删除请求参数
     * @return CmsTemplateResponse 删除响应参数
     * author Taozhouchuan
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除cmsTemplate记录")
    @PostMapping("/deleteCmsTemplate")
    @ResponseBody
    public RestResult<Long> deleteCmsTemplate(@RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        CmsTemplate cmsTemplate = new CmsTemplate();
        BeanUtils.copyProperties(request, cmsTemplate);
        long flag = service.deleteCmsTemplate(cmsTemplate);
        if (flag > 0) {
            return RestResult.success(flag);
        } else {
            return RestResult.error("9999", "删除异常");
        }
    }

    /**
     * 逻辑删除记录
     *
     * @param request 逻辑删除请求参数
     * @return CmsTemplateResponse 逻辑删除响应参数
     * author Taozhouchuan
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "逻辑删除cmsTemplate记录")
    @PostMapping("/deleteCmsTemplateLogically")
    @ResponseBody
    public RestResult<Long> deleteCmsTemplateLogically(@RequestBody CmsTemplateRequest request) throws Throwable {
        //分页处理
        CmsTemplate cmsTemplate = new CmsTemplate();
        BeanUtils.copyProperties(request, cmsTemplate);
        long flag = service.deleteCmsTemplateLogically(cmsTemplate);
        if (flag > 0) {
            return RestResult.success(flag);
        } else {
            return RestResult.error("9999", "逻辑删除异常");
        }
    }
}
